﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity.Model;
using SystemFrameWork.Enums;
using SystemFrameWork;

namespace DutyWeb.Admin
{
    public partial class SysUserList : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            this.PaginationBarSysUserList.PageIndexChanged += new EventHandler(this.Paging);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.PaginationBarSysUserList.PageSize = this.PageSize;
            this.BindSysUserList(ConstantsManager.FirstPage);
            this.PaginationBarSysUserList.PageIndex = ConstantsManager.FirstPage;

            this.PaginationBarSysUserList.AllCount = base.CurrentTotalRecord;
            this.PaginationBarSysUserList.DataBind();
        }

        /// <summary>
        /// bind the question data 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Paging(object sender, EventArgs e)
        {
            this.BindSysUserList(this.PaginationBarSysUserList.PageIndex);
        }

        private void BindSysUserList(int pageIndex)
        {
            SysUserView suv = new SysUserView();

            suv.MyPageModel = new PageModel();
            suv.MyPageModel.PageIndex = pageIndex;
            suv.MyPageModel.PageSize = this.PageSize;
            suv.OrderByField = "ModifiedOn";
            suv.OrderDirection = "desc";

            suv.DepartmentID = this.DepartmentTree1.SelectedDepartmentID;
            if (chkDepartmentContain.Checked)
            {

                if (!string.IsNullOrEmpty(this.DepartmentTree1.SelectedDepartmentName))
                {
                    suv.ParentDNA = this.DepartmentTree1.SelectedParentDNA;
                }
            }

            suv.UserName = this.txtUserName.Text.Trim();
            suv.RealName = this.txtRealName.Text.Trim();
            suv.IDCard = this.txtPoliceNo.Text.Trim();
            suv.Sex = this.rdlSex.SelectedValue;
            List<SysUserView> list = base.sys_UserManagement.LoadSysUserViewByCondition(suv);
            this.gvSysUserList.DataSource = list;
            this.gvSysUserList.DataBind();

            base.CurrentTotalRecord = suv.MyPageModel.TotalRecord;
        }

        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                this.DeleteUsers(null);
                ////bind the paging control
                this.PaginationBarSysUserList.PageSize = this.PageSize;
                this.BindSysUserList(ConstantsManager.FirstPage);
                this.PaginationBarSysUserList.PageIndex = ConstantsManager.FirstPage;

                this.PaginationBarSysUserList.AllCount = base.CurrentTotalRecord;
                this.PaginationBarSysUserList.DataBind();
            }
            catch
            {
                ShowMessage("操作出现异常，请联系管理员!", ExceptionLevel.Info);
                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        private void DeleteUsers(GridViewCommandEventArgs e)
        {
            //if (base.CurrentUserID == Guid.Empty)
            //{
            //    base.ShowMessage("请登录后再操作！", ExceptionLevel.Info);
            //    return;
            //}

            List<Sys_User> lstUser = new List<Sys_User>();

            if (e != null)
            {
                GridViewRow row = ((e.CommandSource as LinkButton).NamingContainer) as GridViewRow;
                int rowIndex = row.RowIndex;
                Guid id = new Guid(this.gvSysUserList.DataKeys[rowIndex].Value.ToString());
                Sys_User ug = new Sys_User();
                ug.ID = id;
                lstUser.Add(ug);
            }
            else
            {
                int flag = 0;
                foreach (GridViewRow myGridViewRow in this.gvSysUserList.Rows)
                {
                    CheckBox myCheckBox = myGridViewRow.FindControl(ConstantsManager.chkSingleKey) as CheckBox;

                    if (myCheckBox.Checked)
                    {
                        flag++;
                        Sys_User ug = new Sys_User();
                        ug.ID = new Guid(this.gvSysUserList.DataKeys[myGridViewRow.DataItemIndex].Value.ToString());
                        ug.CurrentUserID = base.CurrentUserID;
                        lstUser.Add(ug);
                    }
                }


                if (flag == 0)
                {
                    base.ShowMessage("请选择删除项！", ExceptionLevel.Info);
                    return;
                }
            }


            base.sys_UserManagement.Delete(lstUser);

            this.BindSysUserList(ConstantsManager.FirstPage);
        }
    }
}
